package ezxpns.cmd;

import java.awt.event.*;
import java.io.*;
import javax.swing.*;
import javax.swing.filechooser.*;
import ezxpns.Application.*;
import ezxpns.gui.tab.*;
import ezxpns.images.*;

@SuppressWarnings("serial")
public class CmdFile
{
	private AppKey app;
	private int fileNewID=1;
	public FileNew fileNew;
	public FileOpen fileOpen;
	public FileSave fileSave;
	public FileClose fileClose;
	public CmdFile(AppKey app2)
	{
		app=app2;
		fileNew=new FileNew();
		fileOpen=new FileOpen();
		fileSave=new FileSave();
		fileClose=new FileClose();
	}
	public void tabsEnd()
	{
		// Do not change any bit of this code! It is a workaround to scroll to the end of the tab-pane on some systems! //
		if( app.tabs.getTabCount()<=1 ) return;
		Action action=app.tabs.getActionMap().get("scrollTabsForwardAction");
		action.actionPerformed(new ActionEvent(app.tabs,ActionEvent.ACTION_PERFORMED,""));
		app.tabs.getRootPane().setVisible(false);
		app.tabs.setSelectedIndex(0);
		SwingUtilities.invokeLater
		(
			new Runnable()
			{
				public void run()
				{
					Action action=app.tabs.getActionMap().get("navigatePrevious");
					action.actionPerformed(new ActionEvent(app.tabs,ActionEvent.ACTION_PERFORMED,""));
					app.tabs.getRootPane().setVisible(true);
				}
			}
		);
	}
	public class FileNew extends AbstractAction
	{
		FileNew()
		{
			String name="New";
			String desc="New file";
			String key="control N";
			ImageIcon icon=new ImageIcon(Images.get("file/filenew.png"));
			putValue(Action.NAME,name);
			putValue(Action.SHORT_DESCRIPTION,desc);
			putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(key));
			putValue(Action.SMALL_ICON,icon);
			app.root.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),key);
			app.root.getRootPane().getActionMap().put(key,this);
		}
		public void exec()
		{
			try
			{
				final TabGUI tabGUI=new TabGUIDefault();
				app.tabs.addTab("New File "+fileNewID,tabGUI);
				app.cmd.file.fileClose.setEnabled(true);
				app.cmd.file.fileSave.setEnabled(true);
				fileNewID++;
				tabsEnd();
				SwingUtilities.invokeLater
				(
					new Runnable()
					{
						public void run()
						{
							tabGUI.fileNew();
						}
					}
				);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		public void actionPerformed(ActionEvent event)
		{
			exec();
		}
	}
	public class FileOpen extends AbstractAction
	{
		FileOpen()
		{
			String name="Open";
			String desc="Open file";
			String key="control O";
			//ImageIcon icon=new ImageIcon(Images.get("file/Open.png"));
			putValue(Action.NAME,name);
			putValue(Action.SHORT_DESCRIPTION,desc);
			putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(key));
			//putValue(Action.SMALL_ICON,icon);
			app.root.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),key);
			app.root.getRootPane().getActionMap().put(key,this);
		}
		public void exec()
		{
			try
			{
				JFileChooser fileDialog=new JFileChooser();
				FileNameExtensionFilter fileFilter=new FileNameExtensionFilter("EzXpns database","ezx");
				fileDialog.addChoosableFileFilter(fileDialog.getAcceptAllFileFilter());
				fileDialog.addChoosableFileFilter(fileFilter);
				int choice=fileDialog.showOpenDialog(app.root);
				if( choice!=JFileChooser.APPROVE_OPTION ) return;
				final File file=fileDialog.getSelectedFile();
				final TabGUI tabGUI=new TabGUIDefault();
				app.tabs.add(file.getName(),tabGUI);
				app.cmd.file.fileClose.setEnabled(true);
				app.cmd.file.fileSave.setEnabled(true);
				tabsEnd();
				SwingUtilities.invokeLater
				(
					new Runnable()
					{
						public void run()
						{
							tabGUI.fileOpen(file);
						}
					}
				);
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		public void actionPerformed(ActionEvent event)
		{
			exec();
		}
	}
	public class FileSave extends AbstractAction
	{
		FileSave()
		{
			String name="Save";
			String desc="Save file";
			String key="control S";
			putValue(Action.NAME,name);
			putValue(Action.SHORT_DESCRIPTION,desc);
			putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(key));
			app.root.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),key);
			app.root.getRootPane().getActionMap().put(key,this);
			setEnabled(false);
		}
		public boolean exec()
		{
			try
			{
				if( app.tabs.getTabCount()==0 ) return false;
				TabGUI tabGUI=(TabGUI)app.tabs.getComponentAt(app.tabs.getSelectedIndex());
				File file=tabGUI.getFile();
				if( file==null )
				{
					JFileChooser fileDialog=new JFileChooser();
					FileNameExtensionFilter fileFilter=new FileNameExtensionFilter("EzXpns database","ezx");
					fileDialog.addChoosableFileFilter(fileDialog.getAcceptAllFileFilter());
					fileDialog.addChoosableFileFilter(fileFilter);
					int choice=fileDialog.showSaveDialog(app.root);
					if( choice!=JFileChooser.APPROVE_OPTION ) return false;
					file=fileDialog.getSelectedFile();
					app.tabs.setTitleAt(app.tabs.getSelectedIndex(),file.getName());
				}
				tabGUI.fileSave(file);
				return true;
			}
			catch(Exception e)
			{
				e.printStackTrace();
				return false;
			}
		}
		public void actionPerformed(ActionEvent event)
		{
			exec();
		}
	}
	public class FileClose extends AbstractAction
	{
		FileClose()
		{
			String name="Close";
			String desc="Close file";
			String key="control W";
			putValue(Action.NAME,name);
			putValue(Action.SHORT_DESCRIPTION,desc);
			putValue(Action.ACCELERATOR_KEY,KeyStroke.getKeyStroke(key));
			app.root.getRootPane().getInputMap(JComponent.WHEN_IN_FOCUSED_WINDOW).put(KeyStroke.getKeyStroke(key),key);
			app.root.getRootPane().getActionMap().put(key,this);
			setEnabled(false);
		}
		public void exec()
		{
			try
			{
				if( app.tabs.getTabCount()==0 ) return;
				if( ((TabGUI)app.tabs.getSelectedComponent()).getSaved()==false )
				{
					String[] options={"Save","Discard","Cancel"};
					int option=JOptionPane.showOptionDialog
					(
						app.root,
						"Do you want to save the current file?",
						"Close file",
						JOptionPane.YES_NO_CANCEL_OPTION,
						JOptionPane.WARNING_MESSAGE,
						null,
						options,options[0]
					);
					if( option==JOptionPane.YES_OPTION ) if( fileSave.exec()==false ) return;
					if( option==JOptionPane.CANCEL_OPTION ) return;
					if( option==JOptionPane.CLOSED_OPTION ) return;
				}
				app.tabs.removeTabAt(app.tabs.getSelectedIndex());
				if( app.tabs.getTabCount()==0 )
				{
					setEnabled(false);
					app.cmd.file.fileSave.setEnabled(false);
					return;
				}
				if( app.tabs.getSelectedIndex()==app.tabs.getTabCount()-1 ) tabsEnd();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
		public void actionPerformed(ActionEvent event)
		{
			exec();
		}
	}
	public void update()
	{
	}
}
